#include "WebPageSearcher.h"

#include <iostream>
using std::cout;
using std::endl;

WebPageSearcher::WebPageSearcher(/*const TcpConnectionPtr &conn*/){}

WebPageSearcher::~WebPageSearcher(){}

void WebPageSearcher::setKeys(string keys){
    _sought = keys;
}
void WebPageSearcher::setTcpConnectionPtr(const TcpConnectionPtr& conn){
    _conn = conn;
}

void WebPageSearcher::doQuery(){
    WebPageQuery *myQuery = WebPageQuery::getInstance();

    string strResult = myQuery->doQuery(_sought);
    if(strResult.size() == 0){
        strResult = "false";
    }
    strResult += "\n";

    unique_ptr<RedisCache> pRedis(new RedisCache());

    if(!pRedis->connect("127.0.0.1",6379))
    {
        std::cerr << "connect error!" << endl;
        return;
    }

    pRedis->webSet(_sought,strResult);
    //cout<<strResult<<endl;
    //通过conn发送出去
    _conn->sendInLoop("query\n");
    _conn->sendInLoop(strResult); 

}
